 import Cache from "@/utils/cache";
 import {getUserinfo} from "@/http/api/user/user.js"
 import {getCategoryList} from "@/http/api/category/category.js" 
 // #ifndef VUE3
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

const store = new Vuex.Store({
// #endif

// #ifdef VUE3
import {
	createStore
} from 'vuex'
const store = createStore({
	// #endif
	state: {
		//登录用户的信息
		userInfo:JSON.parse(Cache.get('userInfo')||JSON.stringify('')),
		userShopDetails:{},//当前登录店铺信息
		userTutorDetails:{},//当前登录教练信息
		categoryList:[],//分类列表
		isMerchant:'',//0是商家 1不是
		isAssistant:'',//0是教练 1不是
		addressInfo:{
			latitude: '', // 纬度
			longitude: '', // 经度
			name: '', // 名称
			province: '', // 省份
			city: '', // 市区
			district: '', // 区域
			businessAreaName: '', // 商圈
		},//选择的定位数据 
		userId:null,//登录用户id
		//登录后跳转的页面路径 + 页面参数
		returnUrl: "",
		//app版本
		version: "3.0.0",
		//当前是否有网络连接
		networkConnected: true,
		isOnline: true,
		whichEnd: 'index',
		userTabBar: [ 
			{
				"pagePath": "/pages/tutor/tutor",
				"text": "首页",
				"iconPath": "/static/tabbar/h5/icon_shouye.png",
				"selectedIconPath": "/static/tabbar/h5/icon_shouye_active.png",
				"num": 0,
				"isDot": true,
				"verify": true
			},{
				"pagePath": "/pages/index/index",
				"text": "商家",
				"iconPath": "/static/tabbar/h5/sh.png",
				"selectedIconPath": "/static/tabbar/h5/sh_active.png",
				"num": 0,
				"isDot": true,
				"verify": true
			},
			{
				"pagePath": "/pages/find/find",
				"text": "发现",
				"iconPath": "/static/tabbar/h5/icon_faxian.png",
				"selectedIconPath": "/static/tabbar/h5/icon_faxian_active.png",
				"num": 0,
				"isDot": true,
				"verify": true
			},
			{
				"pagePath": "/pages/mine/mine",
				"text": "我的",
				"iconPath": "/static/tabbar/h5/icon_wode.png",
				"selectedIconPath": "/static/tabbar/h5/icon_wode_active.png",
				"num": 0,
				"isDot": true,
				"verify": true
			}
		],
		tabBarIndex: 0
	},
	mutations: {  
		setMerchant(state, val) {
				state.isMerchant = val;
		},
		setUserShopDetails(state, val){
			state.userShopDetails = val;
		},
		//设置当前登录教练端信息 
		setUserTutorDetails(state, val){
			state.userTutorDetails = val;
		},
		//设置分类列表
		setCategoryList(state, val){
			state.categoryList = val;
		},
		clearAdressInfo(state, val){
			state.addressInfo = {
				latitude: '', // 纬度
				longitude: '', // 经度
				name: '', // 名称
				province: '', // 省份
				city: '', // 市区
				district: '', // 区域
				businessAreaName: '', // 商圈
			}
		},
		setAssistant(state, val) {
				state.isAssistant = val;
		},
		setAdressInfo(state, val){
			state.addressInfo = val;
		},
		setUserInfo(state, val) {
				state.userInfo = val;
		},
		changeTabItem(state, item) {
			if (item) {
				state.whichEnd = item
			}
		},
		setTabBarIndex(state, payload) {
			state.tabBarIndex = payload.index
		},
		changeTabBar(state, payload) { 
		 
			const token =Cache.get('access_token')
			// if (!token && (payload.pagePath === '/pages/mine/mine'
			// 		||payload.pagePath === '/pages/find/modules/pushDynamic/pushDynamic')
			// 	) {
			// 	uni.showModal({
			// 		title: '暂未登录',
			// 		content: '请登录',
			// 		confirmText: '前往登录',
			// 		success: function(res) {
			// 			if (res.confirm) {
			// 				uni.reLaunch({
			// 					url: '/pages/mine/modules/userLogin/userLogin'
			// 				})
			// 			} else if (res.cancel) {
			// 				console.log('用户点击取消');
			// 			}
			// 		}
			// 	})
			// 	return false
			// }
			 
				if(payload.pagePath === '/pages/find/modules/pushDynamic/pushDynamic'){
					uni.navigateTo({
						url: '/pages/find/modules/pushDynamic/pushDynamic'
					})
					return false
				}
				if (payload) {
					console.log(payload.pagePath,'switchTabpayload')
					state.tabBarIndex = payload.index
					uni.switchTab({
						url: payload.pagePath
					})
				} else if (payload) {
					state.tabBarIndex = payload.index
				}
				return false
			 
		},
		login(state, payload) {
			if (payload) {
				state.mobile = payload.mobile
			}
			state.isLogin = true
		},
		logout(state) {
			state.mobile = ""
			state.isLogin = false
			state.returnUrl = ""
		},
		setReturnUrl(state, returnUrl) {
			state.returnUrl = returnUrl
		},
		networkChange(state, payload) {
			state.networkConnected = payload.isConnected
		},
		setOnline(state, payload) {
			state.isOnline = state.isOnline
		}
	},
	getters: {
		// 根据 whichEnd 动态选择数据
		tabBar: (state, getters) => {
			let selectedTabBar = [];

			// 根据 whichEnd 的值来选择不同的数据
			//拆分H5
			selectedTabBar = state.userTabBar;
			// 计算并返回tabbar数据
			return selectedTabBar
		},
		// 根据 id 返回分类name
		categoryName: (state) => (id) => {
			const item = state.categoryList.find((item) => item.id === id);
			return item ? item.categoryName : null; // 如果找不到，返回 null
	  },
	},
	actions: {
			getUserInfo(store) { 
					getUserinfo().then((res) => {
						if (res.code == 200) {
							store.commit("setUserInfo", res.data);
							store.commit("setMerchant", res.data&&res.data.isMerchant);
							store.commit("setAssistant", res.data&&res.data.isAssistant);
							Cache.set('userId',res.data&&res.data.id) 
							Cache.set('extendCode',res.data&&res.data.extendCode)
							let info=JSON.stringify(res.data)
							Cache.set('userInfo',info) 
						} else {
							store.commit("setUserInfo", false);
						}
					});
				}, 
			getCategorys(store) { 
				getCategoryList().then((res) => {
					if (res.code == 200) {
						store.commit("setCategoryList", res.data); 
					} else {
						store.commit("setUserInfo", false);
					}
				});
			},
	}
})

export default store